/**
 * @Author:  renyc
 * @Date: 2022年12月13日23:59:21
 */
'use strict';

import ChartWidget from "./ChartWidget";

class BarChartWidget extends ChartWidget{

    constructor(container, options){
        super(container, options);        
    }

    setData(data){
        if(this._echarts){
            let options = this._buildEChartOptions(data);
            if(options){
                this._echarts.setOption(options);
            }
        }
    }

    _buildEChartOptions(data){
        if(!data){
            return;
        }

        let labels = this._buildLabels(data);
        let values = this._buildValues(data);

        return {
            animation: !1,
            /////////////////////////////////////////////////
            //定义echarts的canvas的背景色
            backgroundColor: 'rgba(17, 19, 42, 0.0)',
            /////////////////////////////////////////////////
            //定义titlebar
            // title: [{
            //   id: 'statistic',
            //   text: this._getTitle(),
            // //   right: 120,
            // //   top: 40,
            // //   width: 100,
            //   color: '#fff', 
            //   fontSize: 16
            // }],
            tooltip: { trigger: 'item' },
            //grid: { right: 40, top: 100, bottom: 40, width: '30%' },
            grid : {},
            xAxis: {
                type: 'value',
                scale: !0,
                position: 'top',
                boundaryGap: !1,
                splitLine: { show: !1 },
                axisLine: { show: !1 },
                axisTick: { show: !1 },
                axisLabel: { 
                    margin: 2, 
                    color: '#aaa' 
                }
            },
            yAxis: {
                type: 'category',
                nameGap: 16,
                axisLine: { show: !0, lineStyle: { color: '#ddd' } },
                axisTick: { show: !1, lineStyle: { color: '#ddd' } },
                axisLabel: { 
                    interval: 0, 
                    color: '#ddd' 
                },
                data: labels
            },
            series: [{
                id: 'bar',
                zlevel: 2,
                type: 'bar',
                symbol: 'none',
                itemStyle: { 
                    color: '#ddb926' 
                },
                data: values
            }]
          }
    }

    /**
     * 根据data构造Y轴的Label
     * @param {} data 
     */
    _buildLabels(data){
        let labels = [];
        data.forEach(item=>{
            labels.push(item.name);
        })
        return labels;
    }

    _buildValues(data){
        let values = [];
        data.forEach(item=>{
            values.push(item.value);
        })
        return values;
    }

    _getTitle(){
        return "统计数据"
    }


}

export default BarChartWidget

